package com.zw.demo01.controller;

import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/message")
public class MessageController {

    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication,HttpServletRequest request){
//        返回登录用户的主体信息
//        return authentication.getPrincipal();

//    解析返回jwt信息
        String header_authorization = request.getHeader("Authorization");
        String jwtToken = header_authorization.substring(header_authorization.lastIndexOf("Bearer") + 7);
        return Jwts.parser().setSigningKey("test_key".getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(jwtToken).getBody();
    }

}
